<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<label>
    <input type="text" id="num">
</label>

<button id="btn">点击</button>
<h1 id="result"></h1>

<script>
    const btn = document.getElementById("btn");

    function fn(n) {
        let result = 0
        let i = 1;
        if (n % 2 === 0) i = 2;
        for (i; i <= n; i += 2) {
            result += 1 / i
        }
        return result;
    }

    btn.onclick = function () {
        let num = document.getElementById('num').value
        let result = document.getElementById('result')
        result.innerHTML = '' + fn(num)
    }

</script>

</body>
</html>